<script>
	import api from '@/api/index.js'
	export default {
		 onLaunch: async function (){
			const that = this;
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			console.log('App Launch')
			console.log('load app');
			
			// await that.getCode();
			console.log('getcode')
			uni.removeStorageSync('setting')
			console.log(this.$route.query)
			let uuid = "";
			if(this.$route.query.uuid) {
				uuid = this.$route.query.uuid;
				uni.setStorageSync('uuid', uuid)
			}
			else {
				uuid = uni.getStorageSync('uuid');
			}
			await this.getCode(uuid);
			 
			api.getRequest('index', {
				uuid:uuid,
			}).then((res)=>{
				let response = res.data;
				console.log('set setting')
				uni.setStorageSync('setting', response.setting)
				let setting = response.setting;
				api.getRequest('jssdk', {
					url:window.location.href,
				}).then((res)=>{
					let response = res.data;
					console.log(response)
					console.log(setting)
					window.jWeixin.config(response);
					that.setShare(setting);
				})
				
			})
			//统计数据
			api.getRequest('stat', {
				top:'',
				sub:'',
				argv:''
			});
			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		
		methods:{
			getInfo(){
				console.log('getinfo')
			},
			setShare(setting){
				
				let link = setting.shareLink;
				console.log('setting', link);
				// if(uuid) link+='&uuid='+uuid;
				window.jWeixin.updateAppMessageShareData({ 
					title: setting.shareTitle, // 分享标题
					desc: setting.shareDesc, // 分享描述
					link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: setting.shareImg, // 分享图标
				});
				
				window.jWeixin.updateAppMessageShareData({
					title: setting.shareTitle, // 分享标题
					desc: setting.shareDesc, // 分享描述
					link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: setting.shareImg, // 分享图标
				});
				window.jWeixin.onMenuShareAppMessage({
					title: setting.shareTitle, // 分享标题
					desc: setting.shareDesc, // 分享描述
					link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: setting.shareImg, // 分享图标
				});
				window.jWeixin.onMenuShareTimeline({
					title: setting.shareTitle, // 分享标题
					desc: setting.shareDesc, // 分享描述
					link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: setting.shareImg, // 分享图标
				});
			},
			getCode (uuid) {
				console.log('getCode');
				return new Promise((resolve, reject)=>{
					let auth = this.getUrlCode('auth');
					console.log('auth', auth);
					if(this.isWechat()) {
						
						// if(!auth) {
						// 	return resolve();
						// }
						// else {
						// 	console.log('auth', auth);
						// }
						 
						// 截取地址中的code，如果没有code就去微信授权，如果已经获取到code了就直接把code传给后台获取openId
						let code = this.getUrlCode('code')
						
						if (code === null || code === '') {
							let link = window.location.href;
							let appid = 'wx093f1552fa73e98f';
							// window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri=' + encodeURIComponent('http://127.0.0.1:5173/#/pages/views/profile/login/login') + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
							window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri=' + encodeURIComponent(link) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
							// redirect_uri是授权成功后，跳转的url地址，微信会帮我们跳转到该链接，并且通过？的形式拼接code，这里需要用encodeURIComponent对链接进行处理。
							// 如果配置参数一一对应，那么此时已经通过回调地址刷新页面后，你就会再地址栏中看到code了。
							// http://127.0.0.1/pages/views/profile/login/login?code=001BWV4J1lRzz00H4J1J1vRE4J1BWV4q&state=1
						}
						else{
							api.getRequest('oauth', {
								code:code,
								uuid:uuid,
							}).then((res)=>{
								console.log('token', res.data)
								 uni.setStorageSync('token', res.data.access_token);
								 let interval = setInterval(()=>{
									 if(uni.getStorageSync('token')) {
										 clearInterval(interval);
										 return resolve();
									 }
								 }, 50)
							})
						}
					}
					else{
						return resolve();
					}
					
				})
				
			}, 
			getUrlCode (name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
			},
			
			isWechat() {
				return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
			}
		},
		
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
	
	
	.uni-tabbar .uni-tabbar__icon{
		width: auto !important;
		transform: scale(1.2);
	}
	
	.uni-tabbar .uni-tabbar__icon img{
		width: auto !important;
	}
	.uni-tabbar__label{
		transform: scale(1.2);
	}
	
	#app{
		width: 750rpx;
		display: flex;
		justify-content: center;
	}
	/* #ifdef H5 */
	#app{
		width: 750rpx;
		display: flex;
		justify-content: center;
	}
	
	@media screen and (min-width:750px){
			.uni-tabbar{
				max-width: 750rpx;
				left: 50% !important;
				transform: translate(-50%, 0);
			}
	}
	 
	/* #endif */
	
	
	 
	body{
		display: flex;
		justify-content: center;
	}
	
	 .content-body img{
	 	max-width: 100% !important;
	 		height: auto;
	 }
	 .content-body p{
		 font-size: 25rpx;
	 }
	 
	 
	 .aboutus-body  p span{
	 	font-size: 24rpx !important;
	 }
</style>
